from mxupy import IM, EntityXControl
import OBEvolutionAI as obe


class CollegeControl(EntityXControl):

    class Meta:
        model_class = obe.College

    def get_list(self, select=None, where=None, order_by=None, group_by=None, having=None, limit=100, offset=None, to_dict=False, recurse=False, backrefs=False, max_depth=1):
        """ 获取学院列表 """
        return super().get_list(select=select, where=where, order_by=order_by, group_by=group_by, having=having, limit=limit, offset=offset, to_dict=to_dict, recurse=recurse, backrefs=backrefs, max_depth=max_depth)

    # 删除学院
    def delete(self, model, recursive=True):
        """ 删除学院 """
        college_id = model.get('collegeId') if isinstance(model, dict) else model.collegeId
        
        if college_id == 1:
            return IM().set_error("不能删除默认学院!")

        if (im := obe.DeptControl.inst().exists({"collegeId": college_id})).error:
            return im
        if im.data :
            return IM().set_error("该学校下有系，不能删除！")

        # 调用父类方法时传递recursive参数
        return super().delete(model, recursive)